package com.example.stmp.Controller;

import com.example.stmp.Entity.Department;
import com.example.stmp.Service.Department.DepartmentService;
import com.example.stmp.pojo.PageResult;
import com.example.stmp.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/Department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /*
     * 新增部门
     * */
    @PostMapping
    public Result save(@RequestBody Department department) {

        log.info("新增部门: {}", department);
        departmentService.save(department);
        return Result.success();

    }

    /*
     * 根据id查询部门
     * */
    @GetMapping("/{id}")
    public Result<Department> getById(@PathVariable Long id) {

        log.info("根据id查询部门: {}", id);
        Department department = departmentService.getById(id);
        return Result.success(department);
    }

    /*
     * 部门分页查询
     * */
    @GetMapping("/page")
    public Result<PageResult> page(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                   @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        log.info("分页查询部门: {}", pageNum);
        PageResult pageResult = departmentService.pageQuery(pageNum, pageSize);
        return Result.success(pageResult);
    }

    /*
     * 查询部门信息
     * */
    @GetMapping("/show")
    public Result<List<Department>> list() {

        log.info("查询部门信息");
        List<Department> departmentList = departmentService.list();
        return Result.success(departmentList);

    }

    /*
     * 修改部门信息
     * */
    @PutMapping
    public Result update(@RequestBody Department department) {

        log.info("修改部门信息: {}" , department);
        departmentService.update(department);
        return Result.success();

    }

    /*
     * 删除部门
     * */
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {

        log.info("删除部门: {}", ids);
        departmentService.delete(ids);
        return Result.success();
    }
}
